const curl = require('./curl');
const xlsx = require('./xlsx');
const moment = require('moment');

const get_world_data = function () {
  const url = "https://i.snssdk.com/forum/home/v1/info/?forum_id=1656784762444839";

  return new Promise(function (resolve, reject) {
    curl.download(url, function (res) {
      if (res) {
        let res_json = JSON.parse(res);
        let data_json = JSON.parse(res_json.forum.extra.ncov_string_list);
        console.log('获取头条数据成功！');
        resolve(data_json);
      } else {
        reject('获取头条数据失败！')
      }
    });
  });
}

const get_data = async function (file) {
  let world_data = await get_world_data();

  let update_date = new Date();
  let updateTime = world_data.updateTime;

  // 1- 全国总体情况
  let nt = world_data.nationTotal;
  let ni = world_data.nationwideIncr;
  let nation_today = [
    ['确诊', nt.confirmedTotal],
    ['疑似', nt.suspectedTotal],
    ['治愈', nt.curesTotal],
    ['死亡', nt.deathsTotal],
    ['确诊增加', ni.confirmedIncr],
    ['疑似增加', ni.suspectedIncr],
    ['治愈增加', ni.curesIncr],
    ['死亡增加', ni.deathsIncr],
    ['更新时间', updateTime]
  ];

  // 2-世界情况
  let world_array = [
    ['国家', '确诊', '疑似', '治愈', '死亡', '更新时间']
  ];

  world_data.world.forEach(ele => {
    world_array.push(
      [ele.country, ele.confirmedNum, ele.suspectedNum, ele.curesNum, ele.deathsNum, updateTime]
    );
  });

  // 3- 全国趋势
  let nation_trend_array = [
    ['日期', '确诊', '疑似', '治愈', '死亡', '疑似增加']
  ];

  world_data.nationwide.forEach(ele => {
    nation_trend_array.push([
      ele.date, ele.confirmedNum, ele.suspectedNum, ele.curesNum, ele.deathsNum, ele.suspectedIncr
    ]);
  });


  // 4- 省份情况
  let province_array = [
    ['省份编码', '省份名称', '确诊', '治愈', '死亡', '确诊增加', '更新时间', '更新日期']
  ];

  let cities_array = [
    ['省份编码', '省份名称', '地区编码', '地区名称', '确诊', '治愈', '死亡', '确诊增加', '更新时间', '更新日期']
  ];

  let province_trend_array = [
    ['省份编码', '省份名称', '日期', '确诊', '治愈', '死亡']
  ];

  world_data.provinces.forEach(province => {
    province_array.push([
      province.id, province.name, province.confirmedNum, province.curesNum, province.deathsNum,
      province.confirmedIncr, province.updateTime, province.updateDate
    ]);

    province.series.forEach(s => {
      province_trend_array.push([
        province.id, province.name, s.date, s.confirmedNum, s.curesNum, s.deathsNum
      ]);
    });

    province.cities.forEach(c => {
      cities_array.push([
        province.id, province.name, c.id, c.name,
        c.confirmedNum, c.curesNum, c.deathsNum, c.confirmedIncr, province.updateTime, province.updateDate
      ]);
    });

  });

  let new_file = new xlsx.Xlsx(file);

  new_file.add({
    name: '国际整体情况',
    data: world_array
  });

  new_file.add({
    name: '全国整体情况',
    data: nation_today
  });

  new_file.add({
    name: '全国趋势',
    data: nation_trend_array
  });

  new_file.add({
    name: '省份',
    data: province_array
  });

  new_file.add({
    name: '地市',
    data: cities_array
  });

  new_file.add({
    name: '各省趋势',
    data: province_trend_array
  });

  await new_file.writeFileSync();
}

exports.get_data = get_data;